from pymilvus import MilvusClient, DataType, FieldSchema, CollectionSchema

#
client = MilvusClient(
    uri="http://localhost:19530",
    user="root",
    password="Milvus",
)

schema = MilvusClient.create_schema(
    auto_id=True,
    enable_dynamic_field=True,
    # fields=[
    #     MilvusClient.create_field(name="id", data_type="int64", is_primary=True, auto_id=True),
    #     MilvusClient.create_field(name="embedding", data_type="float_vector", dim=1536)
    # ]
)
schema.add_field(field_name="id", datatype=DataType.INT64, is_primary=True, auto_id=True)
schema.add_field(field_name="vector", datatype=DataType.FLOAT_VECTOR, dim=5)
#schema.add_field(field_name="category", datatype=DataType.VARCHAR, max_length=100)

client.create_collection(
    collection_name="topic_collection",
    schema=schema,
)
res = client.get_collection_stats(
    collection_name="topic_collection"
)
print(res)

res = client.list_collections()
print(res)


index_params = client.prepare_index_params()
index_params.add_index(
    field_name="vector",
    index_type="IVF_FLAT",
    metric_type="COSINE",
    index_name="index_vector",
    params={
        "nlist": 1024
    }
)
client.create_index(
    collection_name="topic_collection",
    index_params=index_params,
    sync=False,
)

res = client.list_indexes(
    collection_name="topic_collection"
)
print(res)
